﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using SMS.Entities.BaseClasses;
using SMS.Entities;
using System.Collections.ObjectModel;
using System.Windows.Data;
using SMS.Common.Silverlight;
using System.ComponentModel;

namespace SMS.ViewModel.Purchase
{
    public class StockMasterViewModel : BaseNotify
    {

        private SMS.Model.StockMasterModel _model = null;

        public StockMasterViewModel()
        {
            NewCommand = new DelegateCommand(OnNew, null);
            SaveCommand = new DelegateCommand(OnSave, null);
            CancelCommand = new DelegateCommand(OnCancel, null);
            LoadCommand = new DelegateCommand(OnLoadCommand, null);
            AddProductCommand = new DelegateCommand(OnAddProductCommand, null);
            if (!DesignerProperties.IsInDesignTool)
            {
                _model = new Model.StockMasterModel();
                _model.OnGetCompleteStockmasterCompleted += new Action<StockMaster>(_model_OnGetCompleteStockmasterCompleted);
                _model.OnLoadStockMasterCompleted += new Action<ObservableCollection<StockMaster>>(_model_OnLoadStockMasterCompleted);
                _model.OnSaveStockMaster += new Action<int>(_model_OnSaveStockMaster);
                _model.OnServiceError += new Action<SMSException>(_model_OnServiceError);
            }
            FromDate = DateTime.Now.Subtract(TimeSpan.FromDays(2));
            EndDate = DateTime.Now;
        }

        void _model_OnServiceError(SMSException obj)
        {
            ErrorMessage = obj.Message;
            UserMessage = string.Empty;
        }

        void _model_OnSaveStockMaster(int obj)
        {
            CompleteStockMaster.Stock_ID = obj;
            UserMessage = "Stock Master Saved Successfully";
            ErrorMessage = string.Empty;
        }

        void _model_OnLoadStockMasterCompleted(ObservableCollection<StockMaster> obj)
        {
            StockList = obj;
            PagedStockList = new PagedCollectionView(StockList);
            UserMessage = "Stock List loaded.";
            ErrorMessage = string.Empty;
        }

        void _model_OnGetCompleteStockmasterCompleted(StockMaster obj)
        {
            CompleteStockMaster = obj;
            UserMessage = "Complete Stock Master loaded.";
        }
        private bool IsLoadComplete = true;
        private DateTime _FromDate;

        public DateTime FromDate
        {
            get { return _FromDate; }
            set { _FromDate = value; OnPropertyChanged("FromDate"); }
        }

        private DateTime _EndDate;

        public DateTime EndDate
        {
            get { return _EndDate; }
            set { _EndDate = value; OnPropertyChanged("EndDate"); }
        }


        private StockMaster _SelectedMaster;

        public StockMaster SelectedMaster
        {
            get { return _SelectedMaster; }
            set
            {
                _SelectedMaster = value;
                OnPropertyChanged("SelectedMaster");
                if (IsLoadComplete && value != null)
                {
                    if (value.Stock_ID > 0)
                    {
                        _model.GetCompleteStockMaster(App.LoggedinUserName, value.Stock_ID);
                        UserMessage = "Getting complete Stock Master...";
                    }
                    else
                    {
                        CompleteStockMaster = value;
                    }
                }
            }
        }

        private StockMaster _CompleteStockMaster;

        public StockMaster CompleteStockMaster
        {
            get { return _CompleteStockMaster; }
            set
            {
                _CompleteStockMaster = value;
                OnPropertyChanged("CompleteStockMaster");
            }
        }


        private ObservableCollection<StockMaster> _StockList;
        public ObservableCollection<StockMaster> StockList
        {
            get { return _StockList; }
            set
            {
                _StockList = value;
                OnPropertyChanged("StockList");
            }
        }

        private PagedCollectionView _PagedStockList;
        public PagedCollectionView PagedStockList
        {
            get { return _PagedStockList; }
            set { _PagedStockList = value; OnPropertyChanged("PagedStockList"); }
        }


        public ICommand NewCommand { get; private set; }
        public ICommand SaveCommand { get; private set; }
        public ICommand CancelCommand { get; private set; }
        public ICommand LoadCommand { get; private set; }
        public ICommand AddProductCommand { get; private set; }

        public void OnNew(object param)
        {
            IsLoadComplete = false;
            if (StockList == null)
                StockList = new ObservableCollection<StockMaster>();
            var newObj = new StockMaster { Stock_Date = DateTime.Now, Date = DateTime.Now };
            StockList.Add(newObj);
            PagedStockList = new PagedCollectionView(StockList);
            PagedStockList.MoveToLastPage();
            IsLoadComplete = true;
            SelectedMaster = newObj;

        }

        private void OnSave(object param)
        {
            if (CompleteStockMaster != null)
            {
                if (CompleteStockMaster.Supplier == null)
                {
                    ErrorMessage = "Please select supplier";
                    return;
                }
                if (CompleteStockMaster.TotalAmount <= 0)
                {
                    ErrorMessage = "Total Amount should be greater than 0";
                    return;
                }
                _model.SaveStockMaster(App.LoggedinUserName, CompleteStockMaster);
                UserMessage = "Saving Stock Master...";
            }
            else
            {
                ErrorMessage = "No StockMaster to save";
            }
        }
        private void OnCancel(object param)
        {
            ViewModel_MainPage.MainPageContext.FrameControl.GoBack();
        }
        public void OnLoadCommand(object param)
        {
            _model.LoadStockMasterList(App.LoggedinUserName, FromDate, EndDate);
            UserMessage = "Loading Stock Master, Please wait...";
        }

        public void OnAddProductCommand(object param)
        {
            ChildWindow chilwindow = new ChildWindow() { Height = App.AppDimension.Height * .9, Width = App.AppDimension.Width * .9 };
            chilwindow.Content = new Views.Purchase.ProductStockWise() { DataContext = new ProductStockWiseViewModel(this.CompleteStockMaster == null ? 0 : this.CompleteStockMaster.Stock_ID) };
            chilwindow.Show();
        }

    }
}
